package com.softeem.score.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softeem.score.common.exception.MyException;
import com.softeem.score.manager.mapper.SysUserMapper;
import com.softeem.score.manager.mapper.TeacherMapper;
import com.softeem.score.manager.service.SysUserService;
import com.softeem.score.manager.service.TeacherService;
import com.softeem.score.model.dto.TeacherDto;
import com.softeem.score.model.entity.school.Teacher;
import com.softeem.score.model.entity.system.SysUser;
import com.softeem.score.model.vo.common.ResultCodeEnum;
import com.softeem.score.model.vo.school.MapVo;
import com.softeem.score.model.vo.school.TeacherVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2024-02-14
 */
@Service
public class TeacherServiceImpl implements TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public PageInfo<TeacherVo> findByPage(TeacherDto teacherDto, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TeacherVo> teacherList = teacherMapper.findByPage(teacherDto);
        return new PageInfo(teacherList);
    }

    @Transactional
    @Override
    public void saveTeacher(Teacher teacher) {
        if (teacherMapper.findTeacherByTeacherNumber(teacher.getTeacherNumber()) > 0) {
            throw new MyException(ResultCodeEnum.TEACHERNUMBER_EXISTS_NOTINSERTED);
        } else {
            teacherMapper.saveTeacher(teacher);
            String md5 = DigestUtils.md5DigestAsHex(teacher.getTeacherNumber().getBytes(StandardCharsets.UTF_8));
            // 判断联系方式是否为手机号或邮箱
            String contactInfo = teacher.getContactInformation();
            String mobile = null;
            String email = null;
            if (isValidPhoneNumber(contactInfo)) {
                mobile = contactInfo;
            } else if (isValidEmail(contactInfo)) {
                email = contactInfo;
            }
            // 创建 SysUser 对象并保存到数据库
            SysUser sysUser = new SysUser(teacher.getTeacherNumber(), md5, mobile, email, null, 0, null, null);
            sysUserService.saveSysUser(sysUser);
            sysUserMapper.bindingRoles(sysUser.getId(), 2L);
        }

    }

    // 判断是否为有效手机号
    private boolean isValidPhoneNumber(String contactInfo) {
        // 定义手机号的正则表达式
        String phoneRegex = "^1[3-9]\\d{9}$";
        // 判断联系方式是否符合手机号格式
        return contactInfo.matches(phoneRegex);
    }

    // 判断是否为有效邮箱
    private boolean isValidEmail(String contactInfo) {
        // 定义邮箱的正则表达式
        String emailRegex = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$";
        // 判断联系方式是否符合邮箱格式
        return contactInfo.matches(emailRegex);
    }

    @Transactional
    @Override
    public void updateSysUser(Teacher teacher) {
        Teacher oldTeacher = teacherMapper.findById(teacher.getId());
        if (!oldTeacher.getContactInformation().equals(teacher.getContactInformation())) {
            sysUserMapper.updateMobile(oldTeacher.getTeacherNumber(), oldTeacher.getContactInformation());
        }
        teacherMapper.updateTeacher(teacher);
    }

    @Transactional
    @Override
    public void deleteTeacherByTeacherNumber(String teacherNumber) {
        teacherMapper.deleteByTeacherNumber(teacherNumber);
        sysUserMapper.deleteSysUserBySysUserName(teacherNumber);
    }

    @Override
    public List<MapVo> findAllTeacher() {
        return teacherMapper.findAll();
    }

    @Override
    public List<MapVo> findTeacherByCourseId(Long courseId) {
        return teacherMapper.findTeacherByCourseId(courseId);
    }

    @Override
    public List<MapVo> getAllTeacherByCollegeId(Long collegeId) {
        return teacherMapper.findAllByCollegeId(collegeId);
    }
}
